package com.itheima.hello.TCP通信.多客户端TCP通信;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;

public class ServerReaderThread extends Thread{
    private Socket socket;
    public ServerReaderThread(Socket socket){
        this.socket=socket;
}

    @Override
    public void run() {
        try (
                InputStream is = socket.getInputStream();
                DataInputStream dis = new DataInputStream(is);
        ) {
            while (true){
                try {
                    String s = dis.readUTF();
                    System.out.println(s);
                    System.out.println(socket.getRemoteSocketAddress());
                    System.out.println("----------------------------");
                } catch (Exception e) {
                    System.out.println(socket.getRemoteSocketAddress() + "下线了!!");
                    System.out.println("----------------------------");
                    dis.close();
                    socket.close();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
